#include "scene.h"
#include "base.h"
#include "text.h"
#include "setting.h"
#include "game.h"
using namespace Ogre;

size_t max(size_t a, size_t b)
{
	return a > b ? a : b;
}

//MouseCursor* mouse_cursor;

void Base::open_scene(bool destroy)
{
	//mouse_cursor = new MouseCursor();
	//mouse_cursor->setImage("sdk_cursor.png");

	OverlayManager& overlay_manager = OverlayManager::getSingleton();

	
	if (destroy)
	{
		Overlay* overlay = overlay_manager.getByName("OverlayName");
		if (overlay == NULL)
			return;
		OverlayContainer* panel = (OverlayContainer*)overlay->getChild("PanelName");
		panel->removeChild("TextAreaName");
		panel->removeChild("TextAreaLoad");
		panel->removeChild("TextAreaInfo");
		overlay->remove2D(panel);
		overlay->clear();
		overlay_manager.destroy(overlay);
		overlay_manager.destroyOverlayElement("TextAreaName");
		overlay_manager.destroyOverlayElement("TextAreaLoad");
		overlay_manager.destroyOverlayElement("TextAreaInfo");
		overlay_manager.destroyOverlayElement("PanelName");
		sounder.stop("menu");
		return;
	}
	if (overlay_manager.getByName("PanelName") != NULL)
		return;
	// Create a panel
	OverlayContainer* panel = static_cast<OverlayContainer*>(
	overlay_manager.createOverlayElement("Panel", "PanelName"));
	panel->setMetricsMode(Ogre::GMM_RELATIVE);
	panel->setPosition(0.1f, 0.1f);
	panel->setDimensions(0.4f, 0.2f);
	//panel->setMaterialName("SdkTrays/Label"); // Optional background material
 
	// Create text area
	TextAreaOverlayElement* textArea = static_cast<TextAreaOverlayElement*>(
	overlay_manager.createOverlayElement("TextArea", "TextAreaName"));
	
	TextAreaOverlayElement* textArea2 = static_cast<TextAreaOverlayElement*>(
	overlay_manager.createOverlayElement("TextArea", "TextAreaLoad"));

	TextAreaOverlayElement* textArea3 = static_cast<TextAreaOverlayElement*>(
	overlay_manager.createOverlayElement("TextArea", "TextAreaInfo"));

	textArea->setMetricsMode(Ogre::GMM_PIXELS);
	textArea->setPosition(0, 0);

	textArea2->setMetricsMode(Ogre::GMM_PIXELS);
	textArea2->setPosition(20, 100);

	
	textArea3->setMetricsMode(Ogre::GMM_PIXELS);
	textArea3->setPosition(20, window->getHeight() - 200);
	
	//MessageBox(NULL,get_text("LOAD").c_str(),"test load",MB_OK);

	textArea->setCaption(get_UTFtext("TEXT_CAP"));
	textArea->setCharHeight(72);
	textArea->setFontName("StarWars");
	textArea->setColourBottom(ColourValue(0.9f, 0.5f, 0.3f));
	textArea->setColourTop(ColourValue(0.5f, 0.0f, 0.7f));

	textArea2->setCaption(get_UTFtext("TEXT_LOAD"));
	textArea2->setCharHeight(32);
	textArea2->setFontName("SdkTrays/Value");
	textArea2->setColourBottom(ColourValue(0.4f, 0.5f, 0.8f));
	textArea2->setColourTop(ColourValue(0.2f, 0.3f, 0.7f));

	textArea3->setCaption(get_UTFtext("TEXT_INFO")+"\n"+get_UTFtext("TEXT_DATE"));
	textArea3->setCharHeight(24);
	textArea3->setFontName("SdkTrays/Value");
	textArea3->setColourBottom(ColourValue(0.4f, 0.5f, 0.8f));
	textArea3->setColourTop(ColourValue(0.2f, 0.3f, 0.7f));
 
	// Create an overlay, and add the panel
	Overlay* overlay = overlay_manager.create("OverlayName");
	overlay->add2D(panel);
 
	// Add the text area to the panel
	panel->addChild(textArea);
	panel->addChild(textArea2);
	panel->addChild(textArea3);

	// Show the overlay
	overlay->show();
	
	if (!load_world())
		throw Ogre::Exception(8, get_text("ERROR_TERRAIN"), "base.cpp, Base::load_terrain");
	if (first_run)
	{
		window->setVisible(false);
	}
}

void Base::select_scene(bool destroy)
{
	static const int SELECT_MENU_WIDTH = 340;
	static const int SELECT_MENU_NUMITEMS = 6;
	static const int SELECT_BUTTON_WIDTH = 120;

	static Ogre::StringVector cam_list;
	static Ogre::StringVector fullscreen_list;
	static Ogre::StringVector FSAA_list;
	static Ogre::StringVector device_list;
	static Ogre::StringVector video_list;

	static OgreBites::Button* btn = (OgreBites::Button*)tray_manager->getWidget("start_select");
	
	if (destroy)
	{
		tray_manager->destroyAllWidgetsInTray(OgreBites::TL_CENTER);
		tray_manager->destroyAllWidgetsInTray(OgreBites::TL_BOTTOMRIGHT);
		btn = NULL;
		cam_list.clear();
		fullscreen_list.clear();
		FSAA_list.clear();
		device_list.clear();
		video_list.clear();
		
		sounder.stop("menu");
		return;
	}

	if (btn)
	{
		return;
	}
	sounder.stop_all();
	sounder.play_repeat("menu");
	
	std::vector<Ogre::String> cam_vs = controler.get_camera_info();
	cam_list = Ogre::StringVector(cam_vs.begin(), cam_vs.end());

	cam_list.push_back(get_text("SEL_KB"));
	//OgreBites::TrayLocation.TL_NONE

	tray_manager->createThickSelectMenu(OgreBites::TL_CENTER,
										"cam_select",
										get_UTFtext("SEL_CAM"),
										SELECT_MENU_WIDTH,
										SELECT_MENU_NUMITEMS,
										cam_list);
	int si = std::find(cam_list.begin(), cam_list.end(), cam_selected)-cam_list.begin();
	if (si >= 0 && si < (signed)(cam_list.size()))
		((OgreBites::SelectMenu*) tray_manager->getWidget("cam_select"))->
		selectItem(si, false);
	else
		((OgreBites::SelectMenu*) tray_manager->getWidget("cam_select"))->
		selectItem(max(0,cam_list.size()-2), false);
	Ogre::RenderSystem* rs = root->getRenderSystem();
	const Ogre::ConfigOptionMap& a = rs->getConfigOptions();
	
	Ogre::ConfigOptionMap::const_iterator it;
	it = a.find(CFG_KEY_FULLSCREEN);
	if (it != a.end())
	{
		fullscreen_list = it->second.possibleValues;

		OgreBites::SelectMenu* sm = (OgreBites::SelectMenu*)
		tray_manager->createThickSelectMenu(OgreBites::TL_CENTER,
											"fullscreen_select",
											get_UTFtext("SEL_FULL"),
											SELECT_MENU_WIDTH,
											SELECT_MENU_NUMITEMS,
											fullscreen_list);

		sm->selectItem(it->second.currentValue, false);
	}
	
	it = a.find(CFG_KEY_VIDEO);
	if (it != a.end())
	{
		video_list = it->second.possibleValues;
		
		OgreBites::SelectMenu* sm = (OgreBites::SelectMenu*)
		tray_manager->createThickSelectMenu(OgreBites::TL_CENTER,
											"video_select",
											get_UTFtext("SEL_VIDEO"),
											SELECT_MENU_WIDTH,
											SELECT_MENU_NUMITEMS,
											video_list);

		sm->selectItem(it->second.currentValue, false);
	}

	it = a.find(CFG_KEY_FSAA);
	if (it != a.end())
	{
		FSAA_list = it->second.possibleValues;
		
		OgreBites::SelectMenu* sm = (OgreBites::SelectMenu*)
		tray_manager->createThickSelectMenu(OgreBites::TL_CENTER,
											"FSAA_select",
											get_UTFtext("SEL_FSAA"),
											SELECT_MENU_WIDTH,
											SELECT_MENU_NUMITEMS,
											FSAA_list);

		sm->selectItem(it->second.currentValue, false);
	}
	
	it = a.find(CFG_KEY_DEVICE);
	if (it != a.end())
	{
		device_list = it->second.possibleValues;
		
		OgreBites::SelectMenu* sm = (OgreBites::SelectMenu*)
		tray_manager->createThickSelectMenu(OgreBites::TL_CENTER,
											"device_select",
											get_UTFtext("SEL_DEVICE"),
											SELECT_MENU_WIDTH,
											SELECT_MENU_NUMITEMS,
											device_list);

		sm->selectItem(it->second.currentValue, false);
	}

	OgreBites::Slider* sl;
	
	sl = (OgreBites::Slider*)
	tray_manager->createThickSlider(OgreBites::TL_CENTER,
											"fx_select",
											get_UTFtext("SEL_VOL"),
											SELECT_MENU_WIDTH,
											60,
											0,
											100,
											101);
	sl->setValue((int)(sounder.get_fx_vol() * 100), false);
	
	sl = (OgreBites::Slider*)
	tray_manager->createThickSlider(OgreBites::TL_CENTER,
											"music_select",
											get_UTFtext("SEL_MUSIC"),
											SELECT_MENU_WIDTH,
											60,
											0,
											100,
											101);
	sl->setValue((int)(sounder.get_music_vol() * 100), false);

	btn = tray_manager->createButton(OgreBites::TL_BOTTOMRIGHT,
									 "start_select",
									 get_text("SEL_OK"),
									 SELECT_BUTTON_WIDTH);
	tray_manager->createButton(OgreBites::TL_BOTTOMRIGHT,
							   "apply_select",
							   get_text("SEL_APPLY"),
							   SELECT_BUTTON_WIDTH);
	tray_manager->createButton(OgreBites::TL_BOTTOMRIGHT,
							   "help_select",
							   get_text("SEL_HELP"),
							   SELECT_BUTTON_WIDTH);
	tray_manager->createButton(OgreBites::TL_BOTTOMRIGHT,
							   "about_select",
							   get_text("SEL_ABT"),
							   SELECT_BUTTON_WIDTH);
	tray_manager->createButton(OgreBites::TL_BOTTOMRIGHT,
							   "exit_select",
							   get_text("SEL_EXIT"),
							   SELECT_BUTTON_WIDTH);
	
	cfg_changed = false;
}



void Base::start_scene(bool destroy)
{
	if (destroy)
	{
		sounder.stop("eng1");
		sounder.stop("eng2");
		sounder.stop("eng3");
		tray_manager->destroyAllWidgets();
		SceneNode* f = scene_manager->getSceneNode("node_fighter");
		f->resetOrientation();
		set_start_pos(scene_manager);
		return;
	}

	SceneNode* g;
	try
	{
		g = scene_manager->getSceneNode("node_carrier");
	}
	catch (Ogre::Exception e)
	{
		
		ParticleSystem* ps = scene_manager->createParticleSystem("nimbus", "Examples/GreenyNimbus");
		scene_manager->getRootSceneNode()->createChildSceneNode("node_target")->attachObject(ps);

		g = scene_manager->getRootSceneNode()->createChildSceneNode("node_carrier");
		SceneNode* gt = g->createChildSceneNode();
		Entity* carrier = scene_manager->createEntity("carrier", CARRIER_FILE_NAME);
		//carrier->setCastShadows(true);
		gt->attachObject(carrier);
		gt->scale(0.01f, 0.01f, 0.01f);
		gt->yaw(Degree(-95));
	}

	set_start_pos(scene_manager);
	SceneNode* f = scene_manager->getSceneNode("node_fighter");
	
	scene_manager->getSceneNode("node_target")->setPosition(f->getPosition());

	f->resetOrientation();
	sounder.play_repeat("eng1");
	sounder.play_repeat("eng2");
	sounder.play_repeat("eng3");

	cam->setPosition(f->getPosition()+Vector3(9000, 4000, 9000));
	cam->lookAt(f->getPosition());

}

void cam_move_near(Camera* cam, Vector3 dest, Real t, Real rate)
{
	t *= rate;
	if (t > 1)
		t = 1;
	cam->move(t * (dest - cam->getPosition()));
}